<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="referrer" content="origin" />
    <meta property="og:description" content="" />
    <meta http-equiv="Cache-Control" content="no-transform" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>html代码编辑器</title>
    <style>
        html,
        body {
            display: block;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
        
        .full {
            display: flex;
            width: 100%;
            height: 100%;
        }
        
        #editor {
            width: 50%;
            height: 100%;
        }
        
        #preview {
            width: 50%;
            height: 100%;
            border-left: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <div class="full">
        <div id="editor">some text</div>
        <div id="preview">
            <iframe frameborder="0" width="100%" height="100%" id="preview-frame"></iframe>
        </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/ace/test/ace.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/ace/test/theme-chrome.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/ace/test/mode-html.min.js" type="text/javascript" charset="utf-8"></script>

    <script>
        var frame = document.getElementById('preview-frame');
        var editor = ace.edit("editor");
        editor.setTheme("ace/theme/chrome");
        var editorMode = ace.require("ace/mode/html").Mode;
        editor.session.setMode(new editorMode());

        editor.commands.addCommand({
            name: 'run',
            bindKey: {
                win: 'Ctrl-S',
                mac: 'Command-S',
                linux: 'Ctrl-S'
            },
            exec: function(editor) {
                frame.contentWindow.window.location.reload()
                localStorage.setItem('ace-editor-content', editor.getValue());
                //frame.contentWindow.window.document.write(editor.getValue());
                //...
            },
            readOnly: true // false if this command should not apply in readOnly mode
        });
        frame.onload = function() {
            frame.contentWindow.window.document.write(editor.getValue());
        }
        if (localStorage.getItem('ace-editor-content')) {
            editor.setValue(localStorage.getItem('ace-editor-content'));
            frame.contentWindow.window.location.reload();
        }
    </script>
</body>

</html>